package com.xjy.rpc.netty;

import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.DefaultThreadFactory;

import java.util.concurrent.*;

/**
 * @description: TODO 自定义线程池  单例： 双锁检查  静态内部类
 * @author XJY
 * @date 2024/10/9 14:08
 * @version 1.0
 */
public class ThreadPool {

    private volatile static ThreadPoolExecutor executor;

    private ThreadPool(){}

    public static ThreadPoolExecutor getThreadPool(){
         if (executor == null){
             synchronized (ThreadPool.class){
                 if (executor == null){
                     executor = new ThreadPoolExecutor(
                             8,
                             17,
                             30,
                             TimeUnit.MINUTES,
                             new LinkedBlockingQueue<>()
                     );
                 }
             }
         }

         return executor;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor pool = ThreadPool.getThreadPool();
        CompletableFuture.runAsync(()-> System.out.println(Thread.currentThread().getName()),pool);
        CompletableFuture.runAsync(()-> System.out.println(Thread.currentThread().getName()),pool);
        CompletableFuture<String> future = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName()), pool)
                .thenApplyAsync(result -> {
                    System.out.println("继续异步执行" + Thread.currentThread().getName());
                    return "success";
                }, pool)
                .thenApply(result -> {
                    System.out.println("继续同步执行" + Thread.currentThread().getName());
                    return result;
                });

        String join = future.join();
        System.out.println(join);

    }
}
